package collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        boolean flag = list.add("hoan");
        boolean flag2 = list.add("dandy");
        System.out.println(flag);
        System.out.println(flag2);
        System.out.println("list.size()---"+list.size());
//        list.add(2,"dokyun"); //index 不能小于0和大于等list.size()
        list.add(2,"dokyun");
        System.out.println("list.size()---"+list.size());
        String str = list.get(2);
        System.out.println(str);

        list.remove("dandy");
        //System.out.println(list.get(1));
        String str04 = list.remove(1);
        //System.out.println(list.get(0));
        System.out.println("----------------");
        System.out.println(list.isEmpty());
        list.clear();
        System.out.println(list.isEmpty());

        System.out.println("----------------");
        list.add("hozin");
        list.add("kite");
        list.add("gucchon");
        list.add("kite");
        list.add("kite");
        list.add("gucchon");
        for(int i = 0; i < list.size(); i++){
            System.out.println(list.get(i));
        }
        String gucchon = list.set(2,"dino");
        System.out.println("被替换元素"+gucchon);
        for(int i = 0; i < list.size(); i++){
            System.out.println(list.get(i));
        }
        System.out.println(list.contains("dino"));//true
        System.out.println(list.contains("dokyun"));//false

        System.out.println(list.indexOf("kite"));
        System.out.println(list.lastIndexOf("kite"));

        System.out.println("--------增强for循环--------");
        for(String l:list){
            System.out.println(l);
        }
        String[] arr = list.toArray(new String[list.size()]);
        System.out.println(Arrays.toString(arr));
        for(String s:arr){
            System.out.println(s);
        }

        System.out.println("-------------元素并集------------");
        List<String> a = new ArrayList<>();
        a.add("a");
        a.add("b");
        a.add("c");

        ArrayList<String> b = new ArrayList<>();
        b.add("b");
        b.add("c");
        b.add("d");
        //a并b
        boolean flag6 = a.addAll(b);
        System.out.println(flag6);
        for(String s:a){
            System.out.println(s);
        }

        System.out.println("-------------元素交集------------");
        List<String> a1 = new ArrayList<>();
        a1.add("a");
        a1.add("b");
        a1.add("c");

        List<String> b1 = new ArrayList<>();
        b1.add("b");
        b1.add("c");
        b1.add("d");

        boolean flag7 = a1.retainAll(b1);
        for (String s :
                a1) {
            System.out.println(s);

        }

        System.out.println("-------------元素差集------------");
        List<String> a2 = new ArrayList<>();
        a2.add("a");
        a2.add("b");
        a2.add("c");

        List<String> b2 = new ArrayList<>();
        b2.add("b");
        b2.add("c");
        b2.add("d");

        boolean flag8 = a2.removeAll(b2);
        for (String s :
                a2) {
            System.out.println(s);
        }

    }
}
